<template>
	<view>
		<uni-section title="搜索" type="line">
			<uni-search-bar @confirm="search" :focus="true" v-model="params.num" @blur="blur" 
				@cancel="cancel" @clear="clear">
			</uni-search-bar>
			<view class="uni-padding-wrap uni-common-mt">
				<uni-segmented-control :current="current" active-color="#007aff"
					:values="items" @clickItem="onClickItem" />
			</view>
		</uni-section>
		<uni-section title="资产列表" type="line">
			<uni-list>
				<uni-list-item v-for="(item, i) in list" :key="i" clickable
					:title="item.assetNum" :note="item.remark" show-arrow @click="showDetail(item)"
					:rightText="dictTag('asset_status',item.status)">			
				</uni-list-item>
			</uni-list>
		</uni-section>
		<uni-load-more :status="moreStatus" @clickLoadMore="getList"/>
	</view>
</template>

<script>
	import {listStockDetail} from '@/api/stock.js'
	import { dictMixin } from '@/plugins/dict.js';
	export default {
		mixins: [dictMixin],
		data() {
			return {
				items: ['未盘点', '已盘点'],
				list:[],
				current: 0,
				action: 'view',
				stockId: 0,
				moreStatus:'noMore',
				params:{
					num:null,
					status: null,
					stockId: 0,
					pageSize: 10,
					pageNum: 1,
					orderByColumn: 't.id',
					isAsc: 'DESC'
				}
			}
		},
		onLoad() {			
			if(this.$route.query.stockId){
				this.params.stockId = this.$route.query.stockId;
			}
			if(this.$route.query.action){
				this.action = this.$route.query.action;
				if(this.action == 'stock'){
					this.params.status = 'none';
				}
			}			
			
			this.useDict(['asset_status', 'sys_yes_no'], ()=>{	
				if(this.action == 'view'){
					this.items = ['全部'];
					this.dictOptions['asset_status'].forEach(dd=>{
						this.items.push(dd.dictLabel);
					})
				}
				console.log('init dict')
			});
			
			this.getList();
		},
		methods: {
			getList() {
				this.moreStatus = 'loading';
				listStockDetail(this.params).then(response=>{
					if(this.params.pageNum == 1){
						this.list = response.rows;
					}else{
						this.list = this.list.concat(response.rows);
					}
					
					if(response.total > this.params.pageNum * this.params.pageSize){
						this.moreStatus = 'more';
						this.params.pageNum += 1;
					}else{						
						this.moreStatus = 'noMore';
					}
				})
			},		
			onClickItem(e) {
				if(e.currentIndex == 0){
					this.params.status = 'none';
				}else if(e.currentIndex == 1){
					this.params.status = 'stock';
				}
				this.params.pageNum = 1;
				this.getList();
			},
			search(res) {
				this.params.pageNum = 1;
				this.getList();
			},
			clear(res) {
				this.params.pageNum = 1;
				this.getList();
			},
			blur(res) {
				this.params.pageNum = 1;
				this.getList();
			},
			showDetail(item) {		
				this.$tab.navigateTo('/pages/stock/detail?action='+this.action+'&id='+item.id);		
			}
		}
	}
</script>

<style>
	.uni-common-mt {
		margin-top: 10px;
	}

	.uni-padding-wrap {
		// width: 750rpx;
		padding: 0px 10px;
	}
</style>